package com.hroze.util.web;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import com.hroze.util.JsonUtil;

/**
 * @author zhwang 2009-11-13 Properties工具类
 * @version 1.0  2009-12-17
 */
public class Prop {
	/**
	 * 默认配置文件放置路径
	 */
	public static final String defaultDir = "/config";
	/**
	 * 配置文件扩展名
	 */
	public static final String extName = ".properties";
	private Properties p;
	private String path;

	/**
	 * 从路径新建一个Properties对象
	 * 
	 * @param path
	 */
	public Prop(String path) {
		this.path = path;
		this.load();
	}

	/**
	 * 从类创建
	 * 
	 * @param clazz
	 */
	public Prop(Class clazz) {
		this.path = getPathByClass(clazz);
		this.load();
	}

	/**
	 * 读取或者保存 如果 p不存在，从路径新建一个 如果 p存在，将p保存到path上
	 * 
	 * @param p
	 * @param path
	 */
	public Prop(Properties p, String path) {
		this.path = path;
		if (p == null) {
			this.load();
		} else {
			this.p = p;
			this.save();
		}
	}

	public void load() {
		p = load(path);

	}
	

	public void reload() {
		this.load();
	}

	/**
	 * 获得一个值
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key) {
		return p.getProperty(key);
	}

	/**
	 * 获得一个值
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key, String defaultValue) {
		return p.getProperty(key, defaultValue);
	}

	public Float getFloat(String key) {

		Float i = null;
		try {
			i = Float.parseFloat(p.getProperty(key));
		} catch (NumberFormatException e) {

		} catch (NullPointerException ex) {

		}
		return i;

	}

	public Double getDouble(String key) {

		Double i = null;
		try {
			i = Double.parseDouble(p.getProperty(key));
		} catch (NumberFormatException e) {

		} catch (NullPointerException ex) {

		}
		return i;

	}

	/**
	 * 返回string 数组
	 * 
	 * @param key
	 * @return
	 */
	public String[] getArray(String key) {
		String value = p.getProperty(key);
		if (value != null) {
			return value.split(",");
		}
		return null;

	}

	public void set(String key, Collection<String> value) {
		this.set(key, StringUtils.join(value, ","));
	}

	public void putAll(Map<String, String> value) {
		this.p.putAll(value);
	}

	public Long getLong(String key) {

		Long i = null;
		try {
			i = Long.parseLong(p.getProperty(key));
		} catch (NumberFormatException e) {

		} catch (NullPointerException ex) {

		}
		return i;

	}

	public Integer getInt(String key) {
		Integer i = null;
		try {
			i = Integer.parseInt(p.getProperty(key));
		} catch (NumberFormatException e) {

		} catch (NullPointerException ex) {

		}
		return i;
	}
	/**
	 * 获得map列表,不存在是返回空列表
	 * @param key
	 * @return
	 */
	public List<Map> getMapList(String key){
		String v=p.getProperty(key);
		if(StringUtils.isBlank(v)){
			return null;
		}
		return  (List<Map>)JsonUtil.jsonToObj(v);
	}

	/**
	 * 更改或增加属性值
	 * 
	 * @param key
	 * @param value
	 */
	public void set(String key, String value) {
		p.setProperty(key, value);
	}

	/**
	 * 更改或增加属性值
	 * 
	 * @param key
	 * @param value
	 */
	public void set(String key, Object value) {
		this.set(key, value.toString());
	}

	/**
	 * 删除属性
	 * 
	 * @param key
	 * @param value
	 */
	public void remove(String key) {
		p.remove(key);
	}

	/**
	 * 保存值
	 */
	public void save() {
		save(p, path);
	}

	/**
	 * 另存为一个路径
	 * 
	 * @param path
	 */
	public void save(String path) {
		save(p, path);
	}

	/**
	 * 销毁实例
	 */
	public void destroy() {
		this.path = null;
		this.p = null;
	}

	/**
	 * a shortName for getRealPathProperties
	 * 
	 * @param path
	 * @return
	 */
	public static Properties load(String path) {
		Properties props = new Properties();
		try {
			File f = getFile(path);
			FileReader reader = new FileReader(f);
			props.load(reader);
		} catch (IOException e) {
			System.err.println("属性文件读取出错！");
			e.printStackTrace();
			props = null;
		}

		return props;
	}

	public static File getFile(String path) {
		File f = new File(path);
		File parent = f.getParentFile();
		if (!parent.exists()) {
			parent.mkdirs();
		}
		if (!f.exists()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				return null;
			}
		}
		return f;
	}

	/**
	 * 保存配置文件
	 * 
	 * @param p
	 * @param path
	 */
	public static void save(Properties p, String path) {
		try {
			File f = getFile(path);
			FileWriter out = new FileWriter(f);
			p.store(out, "Properties file:" + f.getAbsolutePath());
		} catch (IOException e) {
			System.err.println("属性文件保存出错！");
			e.printStackTrace();
		}
	}

	public static String getPathByClass(Class clazz) {
		String path = null;
		String className = clazz.getCanonicalName().replace('.', '/') + extName;
		String shortName = clazz.getSimpleName().replace('.', '/') + extName;
		String defaultPath = defaultDir + File.separator + shortName;
		// 默认路径下路径
		defaultPath = SystemConfig.getClassRealPath(defaultPath);
		// 转化为包下的绝对路径
		className = SystemConfig.getClassRealPath("/" + className);
		// 转化为绝对路径
		shortName = SystemConfig.getClassRealPath("/" + shortName);
		File f = new File(className);
		// 和类同一路径一下是不是存在
		if (f.exists()) {
			path = className;
		} else {
			File shortFile = new File(shortName);
			if (shortFile.exists()) {
				path = shortName;
			} else {
				path = defaultPath;
			}

		}
		return path;
	}

	public static void main(String[] args) {
		Prop p = new Prop("D:/a.properties");
		p.set("test", "dfafas");
	}

	public Properties getP() {
		return p;
	}

	public void setP(Properties p) {
		this.p = p;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
}
